package com.taobaolite.item;

import com.taobaolite.cart_has_items.CartHasItems;
import com.taobaolite.history.History;
import com.taobaolite.order_has_items.OrderHasItems;
import com.taobaolite.user.store.Store;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;
import org.springframework.context.annotation.DependsOn;

import javax.persistence.*;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.Set;

@Table(name = "item")
@Entity
@DependsOn("Store")
public class Item {
    @Id
//    @SequenceGenerator(
//            name = "item_id_generator",
//            sequenceName = "item_id_sequence",
//            initialValue = 59309201, // 8 digits
//            allocationSize = 1
//    )
//    @GeneratedValue(
//            strategy = GenerationType.AUTO,
//            generator = "item_id_generator"
//    )
    private Long itemId;
    @Column(nullable = false,unique = true)
    private String name;
    @Column(nullable = false)
    private Double price;
    @Column(nullable = false)
    private String description;
    @Column(nullable = false)
    private String imageDir;
    @NotNull
    @Min(value = -1)
    private Long amount;

    @OneToMany(cascade = CascadeType.ALL)
    @OnDelete(action = OnDeleteAction.CASCADE)
    @JoinColumn(name = "itemId")
    private Set<CartHasItems> cartHasItems;

    @OneToMany(cascade = CascadeType.DETACH)
    @OnDelete(action = OnDeleteAction.CASCADE)
    @JoinColumn(name = "itemId")
    private Set<History> History;

    @OneToMany(cascade = CascadeType.ALL)
    @OnDelete(action = OnDeleteAction.NO_ACTION)
    @JoinColumn(name = "itemId")
    private Set<OrderHasItems> orderHasItems;

    @ManyToOne
    @JoinColumn(name = "storeId", nullable = false)
    private Store store;

    public Item() {}

    public Set<CartHasItems> getCartHasItems() {
        return cartHasItems;
    }

    public void setCartHasItems(Set<CartHasItems> cartHasItems) {
        this.cartHasItems = cartHasItems;
    }

    public Set<com.taobaolite.history.History> getHistory() {
        return History;
    }

    public void setHistory(Set<com.taobaolite.history.History> history) {
        History = history;
    }

    public Set<OrderHasItems> getOrderHasItems() {
        return orderHasItems;
    }

    public void setOrderHasItems(Set<OrderHasItems> orderHasItems) {
        this.orderHasItems = orderHasItems;
    }

    public Long getItemId() {
        return itemId;
    }

    public void setItemId(Long itemId) {
        this.itemId = itemId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getImageDir() {
        return imageDir;
    }

    public void setImageDir(String imageDir) {
        this.imageDir = imageDir;
    }

    public Long getAmount() {
        return amount;
    }

    public void setAmount(Long amount) {
        this.amount = amount;
    }

    public Store getStore() {
        return store;
    }

    public void setStore(Store store) {
        this.store = store;
    }

    public Item(Long itemId, String name, Double price, String description, Long amount, Store store) {
        this.itemId = itemId;
        this.name = name;
        this.price = price;
        this.description = description;
        this.imageDir = "items/" + this.itemId;
        this.amount = amount;
        this.store = store;
    }
}
